package com.yiqi.liebang.feature.home.view;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.baiiu.filter.DropDownMenu;
import com.baiiu.filter.interfaces.OnFilterDoneListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.widget.filter.DropMenuAdapter;
import com.yiqi.liebang.common.widget.filter.entity.FilterUrl;
import com.yiqi.liebang.entity.bo.TopicBo;
import com.yiqi.liebang.entity.enums.CertificateImage;
import com.yiqi.liebang.feature.home.contract.TopicContract;
import com.yiqi.liebang.feature.home.dependencies.topic.DaggerTopicComponent;
import com.yiqi.liebang.feature.home.dependencies.topic.TopicPresenterModule;
import com.yiqi.liebang.feature.home.view.adapter.TopicAdapter;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;
import com.yiqi.liebang.feature.setting.view.ImageActivity;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;


public class Topic2Fragment extends BaseFragment implements OnFilterDoneListener, TopicContract.View, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener {

    @BindView(R.id.dropDownMenu)
    DropDownMenu mDropDownMenu;

    @Inject
    TopicContract.Presenter mPresenter;

    @BindView(R.id.mFilterContentView)
    RecyclerView mMFilterContentView;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;
    private int mCurrentCounter = 0;

    private boolean isErr;
    private boolean mLoadMoreEndGone = false;

    private int pageSize = 10;
    private int pageNow = 1;
    private String mClassifyId, mClassifyName = null;
    private String mClassify2Id = null;
    private String mServiceType = null;
    private String mSortType = null;
    TopicAdapter mAdapter;
    private List<TopicBo> mData;
    private EmptyView mEmptyView;


    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_topic2;
    }

    @Override
    protected void initView() {
        initAdapter();
        mClassifyId = getActivity().getIntent().getStringExtra("type_id");
        mClassify2Id = getActivity().getIntent().getStringExtra("type_id2");
        mClassifyName = getActivity().getIntent().getStringExtra("type_name");
        initFilterDropDownView();


    }


    @Override
    protected void initInjector() {
        DaggerTopicComponent.builder().topicPresenterModule(new TopicPresenterModule(this)).build().inject(this);
    }


    @Override
    protected void initData() {
        pageNow = 1;
        mPresenter.getTopicList(TextUtils.isEmpty(mClassifyId) ? null : mClassifyId, mServiceType, TextUtils.isEmpty(mClassify2Id) ? null : mClassify2Id, mSortType, pageSize, pageNow);
    }

    @Override
    protected void initEvent() {

    }

    private void initAdapter() {
        mSmartRefesh.setEnableLoadMore(false);
        mEmptyView = new EmptyView(mMFilterContentView);
        mMFilterContentView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mMFilterContentView.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mSmartRefesh.setOnRefreshListener(this);
        mAdapter = new TopicAdapter();
        mAdapter.bindToRecyclerView(mMFilterContentView);
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
    }


    private void initFilterDropDownView() {
        String[] titleList = new String[]{TextUtils.isEmpty(mClassifyName) ? "全部分类" : mClassifyName, "服务类型", "智能排序"};
        mDropDownMenu.setMenuAdapter(new DropMenuAdapter(getActivity(), titleList,mClassifyId, mClassify2Id, this, true));
//        if (mDropDownMenu.isClosed()&&!TextUtils.isEmpty(mClassifyId)){
//            mDropDownMenu.show();
//        }
    }


    @Override
    public void onStop() {
        super.onStop();
        FilterUrl.instance()
                .clear();

    }

    @Override
    public void onFilterDone(int position, String positionTitle, String urlValue) {
        mDropDownMenu.setPositionIndicatorText(FilterUrl.instance().position, FilterUrl.instance().positionTitle == null ? "" : FilterUrl.instance().positionTitle.getClassify());
        mDropDownMenu.close();
        switch (FilterUrl.instance().position) {
            case 0:
                if (FilterUrl.instance().doubleListRight != null && !TextUtils.isEmpty(FilterUrl.instance().doubleListRight.getId())) {
                    mClassify2Id = FilterUrl.instance().doubleListRight.getId();
                    mClassifyId = null;
                } else if (FilterUrl.instance().doubleListLeft != null) {
                    mClassifyId = FilterUrl.instance().doubleListLeft.getId();
                    mDropDownMenu.setPositionIndicatorText(0, FilterUrl.instance().doubleListLeft.getClassify());
                    mClassify2Id = null;
                }
                break;
            case 1:
                if (FilterUrl.instance().singleServicePosition != null) {
                    mServiceType = FilterUrl.instance().singleServicePosition.getId();
                }
                break;
            case 2:
                if (FilterUrl.instance().singleSortPosition != null) {
                    mSortType = FilterUrl.instance().singleSortPosition.getId();
                }
                break;

        }

        initData();

    }

    @Override
    public void showTopicListSuccess(List<TopicBo> workBoPageData) {
        mSmartRefesh.finishRefresh();
        mData = workBoPageData;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showTopicListError(String error) {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
        mAdapter.setEnableLoadMore(true);
        mSmartRefesh.finishRefresh();
    }

    @Override
    public void showTopicListMoreSuccess(List<TopicBo> msg) {
        setData(false, msg);
    }

    @Override
    public void showTopicListMoreError(String msg) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void showTopicListEmpty() {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refresh();
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

    private void refresh() {
        mAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNow = 1;
        mPresenter.getTopicList(mClassifyId, mServiceType, mClassify2Id, mSortType, pageSize, pageNow);
    }

    private void loadMore() {
        mPresenter.getTopicListMore(mClassifyId, mServiceType, mClassify2Id, mSortType, pageSize, pageNow);
    }

    private void setData(boolean isRefresh, List data) {
        pageNow++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(getActivity(), TopicInfoActivity.class);
        intent.putExtra("topic", mAdapter.getData().get(position).getId());
        intent.putExtra("user_id", mAdapter.getData().get(position).getUserAccountFormMap().getUserUid());
        this.startActivity(intent);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.iv_user_topic:
                Intent intent = new Intent(getActivity(), UserInfoActivity.class);
                intent.putExtra("is_view_my", false);
                intent.putExtra("visitorUid", mAdapter.getData().get(position).getUserAccountFormMap().getUserUid());
                startActivity(intent);
                break;
            case R.id.iv_user_position:
                Intent intentWork = new Intent(getActivity(), ImageActivity.class);
                intentWork.putExtra("type", CertificateImage.ZHIYE.getIndex());
                intentWork.putExtra("userid", mAdapter.getData().get(position).getUserAccountFormMap().getUserUid());
                startActivity(intentWork);
                break;
            case R.id.iv_user_certification:
                Intent intentInfo = new Intent(getActivity(), ImageActivity.class);
                intentInfo.putExtra("type", CertificateImage.BASIC.getIndex());
                intentInfo.putExtra("userid", mAdapter.getData().get(position).getUserAccountFormMap().getUserUid());
                startActivity(intentInfo);
                break;
        }
    }
}
